<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function JewelryStore(uname, address, products = []) {
        this.uname = uname
        this.address = address
        this.products = products
      }

      function Product(name, pic) {
        this.name = name
        this.pic = pic
      }

      JewelryStore.prototype.listProducts = function () {
        this.products
          .map((item) => {
            return console.log(`商品名称：${item.name},价格：${item.pic}`)
          })
          .join('')
      }

      //添加商品
      JewelryStore.prototype.addProduct = function (product) {
        // console.log(product)
        if (!product.name || !product.pic) {
          throw new Error('请填写商品名字和价格！')
        } else {
          this.products.push(product)
        }
      }

      //删除
      JewelryStore.prototype.removeProduct = function (product) {
        const index = this.products.findIndex((item) => {
          if (item.name === product) return item.name
        })
        if (index === -1) {
          throw new Error('未找到！')
        }
        return this.products.splice(index, 1)
      }
      //总和
      JewelryStore.prototype.calculateTotalSales = function () {
        console.log(this.products)
        const num = this.products.reduce((prve, item) => {
          return prve + item.pic
        }, 0)
        return num
      }

      const store = new JewelryStore('瓜皮的超市', '杭州市', [
        new Product('蓝电霸王龙头骨', 50),
        new Product('蓝银皇头骨', 25),
      ])

      // store.listProducts()
      //添加
      store.addProduct(new Product('佛怒火莲', 100))
      store.addProduct(new Product('青莲地心火', 200))

      // store.removeProduct('佛怒火莲')
      console.log(store.calculateTotalSales())
      console.log(store)
    </script>
  </body>
</html>
